package cn.iocoder.yudao.module.erp.service.product;

import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductInventoryRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductInventorySaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductInventoryInfoDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductPurchaseInfoDO;
import cn.iocoder.yudao.module.erp.dal.mysql.product.ErpProductInventoryMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * 物料库存属性 Service 实现类
 *
 * @author lekaihuai
 */
@Service
@Validated
public class ErpProductInventoryServiceImpl implements ErpProductInventoryService {

    @Resource
    private ErpProductInventoryMapper inventoryMapper;

    @Override
    public Long createErpProductInventory(ErpProductInventorySaveReqVO createReqVO) {
        // 插入
        ErpProductInventoryInfoDO inventory = BeanUtils.toBean(createReqVO, ErpProductInventoryInfoDO.class);
        inventoryMapper.insertOrUpdate(inventory);
        // 返回
        return inventory.getFid();
    }

    @Override
    public void updateErpProductInventory(ErpProductInventorySaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialinventoryExists(updateReqVO.getFid());
        // 更新
        ErpProductInventoryInfoDO updateObj = BeanUtils.toBean(updateReqVO, ErpProductInventoryInfoDO.class);
        inventoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteErpProductInventory(Long id) {
        // 校验存在
        validateMaterialinventoryExists(id);
        // 删除
        inventoryMapper.deleteById(id);
    }

    private void validateMaterialinventoryExists(Long id) {
        if (inventoryMapper.selectById(id) == null) {
            throw exception(MATERIALINVENTORY_NOT_EXISTS);
        }
    }

    @Override
    public ErpProductInventoryInfoDO getErpProductInventory(Long id) {
        return inventoryMapper.selectOne(ErpProductInventoryInfoDO::getFmaterialid, id);
    }

    @Override
    public List<ErpProductInventoryInfoDO> getErpProductInventoryList(Collection<Long> ids){
        return inventoryMapper.selectBatchIds(ids);
    }

}